package hr.pzrinscak.web;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.activiti.engine.IdentityService;
import org.activiti.engine.RepositoryService;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.TaskService;
import org.activiti.engine.identity.Group;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class TaskTreeNavigationController {

    private final static Logger LOGGER = LoggerFactory.getLogger(TaskTreeNavigationController.class);

    @Autowired
    TaskService taskService;

    @Autowired
    RepositoryService repositoryService;

    @Autowired
    IdentityService identityService;

    @Autowired
    RuntimeService runtimeService;

    @RequestMapping(value = "/navigation/tree", method = RequestMethod.GET)
    public @ResponseBody
    List<Map<String, Object>> getNodes(@RequestParam(required = false) final String assignee, @RequestParam(required = false) final String candidate,
            @RequestParam(required = false) final String candidateGroup) {

        List<Map<String, Object>> response = new ArrayList<Map<String, Object>>(2);

        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        String userId = authentication.getName();

        List<Group> userGroups = identityService.createGroupQuery()
                                                .groupMember(userId)
                                                .groupType("assignment")
                                                .orderByGroupName()
                                                .asc()
                                                .list();
        List<Map<String, Object>> groupList = new ArrayList<Map<String, Object>>(userGroups.size());
        for (Group group : userGroups) {
            long taskCount = taskService.createTaskQuery()
                                    .taskCandidateGroup(group.getId())
                                    .count();
            Map<String, Object> groupNode = new HashMap<String, Object>(5);
            groupNode.put("id", group.getId());
            groupNode.put("text", "in " + group.getName() + " (" + taskCount + ")");
            groupNode.put("leaf", true);
            groupNode.put("iconCls", "icon-tree-node-group");
            groupNode.put("cls", "tree-node");
            groupList.add(groupNode);
        }

        long assignedTasksCount = taskService.createTaskQuery()
                                                .taskAssignee(userId)
                                                .count();
        Map<String, Object> parentNode = new HashMap<String, Object>(1);
        parentNode.put("id", "assigned" );
        parentNode.put("text", "My tasks (" + assignedTasksCount + ")" );
        parentNode.put("leaf", true);
        parentNode.put("iconCls", "icon-tree-node-mytasks");
        parentNode.put("cls", "tree-node");
        response.add(parentNode);

        long unassignedTasksCount = taskService.createTaskQuery()
                                                .taskCandidateUser(userId)
                                                .count();
        parentNode = new HashMap<String, Object>(1);
        parentNode.put("id", "unassigned");
        parentNode.put("text", "Unassigned tasks (" + unassignedTasksCount + ")" );
        parentNode.put("leaf", userGroups.isEmpty());
        parentNode.put("iconCls", "icon-tree-node-unassigned");
        parentNode.put("cls", "tree-node");
        parentNode.put("children", groupList);
        response.add(parentNode);

        return response;

    }
}
